﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;



namespace EntranceApp
{
    class Entrance
    {
        private DBhelper dataHelper;
        private MySqlConnection MySqlconnection;
        private Client client;

        public Entrance()
        {
            dataHelper = new DBhelper();
            MySqlconnection = new MySqlConnection(dataHelper.SqlString);
        }



        public Client SearchClient(string rfid)
        {
            string sql = "SELECT * FROM client WHERE rfid =\"" + rfid + "\";";
            MySqlCommand command = new MySqlCommand(sql, MySqlconnection);

            try
            {
                MySqlconnection.Open();
                MySqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                   string first_name = Convert.ToString(reader["first_name"]);
                    string last_name = Convert.ToString(reader["last_name"]);
                    string account_id = (reader["account_id"]).ToString();
                    double balance = Convert.ToDouble(reader["balance"]);
                    DateTime date = Convert.ToDateTime(reader["date_birth"]);
                    client = new Client(first_name, last_name, account_id, balance, date);

                }

            }

            catch (MySqlException ex)
            {
                System.Windows.Forms.MessageBox.Show("The RFID is not in the database\n Exeption: " + ex.ToString());
            }
            finally
            {
                MySqlconnection.Close();
            }
            return client;
        }
        public bool InEvent(string account_id, int i)
        {
            string sql = "UPDATE client SET in_event = " + i + "\n WHERE account_id = '" + account_id + "';";
            MySqlCommand command = new MySqlCommand(sql, MySqlconnection);
            try
            {
                MySqlconnection.Open();
                MySqlDataReader reader = command.ExecuteReader();
            }
            catch (MySqlException ex)
            {
                System.Windows.Forms.MessageBox.Show("It is not possible to update : " + ex.ToString());
            }
            finally
            {
                MySqlconnection.Close();
            }
            return true;
        }
        public void RechargeMoneyBalance(double amountOfMoney, string account_id)
        {
            string sql = "UPDATE client SET balance = balance  + " + amountOfMoney + " WHERE account_id = '" + account_id  + "';";
            MySqlCommand command = new MySqlCommand(sql, MySqlconnection);
            try
            {
                MySqlconnection.Open();
                MySqlDataReader reader = command.ExecuteReader();

            }

            catch (MySqlException ex)
            {
                System.Windows.Forms.MessageBox.Show("bad " + ex.Message); ;
            }
            finally
            {
                MySqlconnection.Close();
            }
        }
        public int CheckInEvent(string account_id)
        {
            int i = 0;
            string sql = "SELECT in_event FROM client WHERE account_id =\"" + account_id + "\";";
            MySqlCommand command = new MySqlCommand(sql, MySqlconnection);

            try
            {
                MySqlconnection.Open();
                MySqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    i = Convert.ToInt32(reader["in_event"]);

                }

            }

            catch (MySqlException ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
            finally
            {
                MySqlconnection.Close();
            }
            return i;

        }


    }
}
